//运营管理相关

/**
 * 奖励物品select改变,动态修改输入框
 * 
 * @param obj
 */
function rewardTypeChage(obj) {
	// selectedIndex
	if (obj.value == "道具") {
		//
		$("#reward_args_label", navTab.getCurrentPanel()).html(
				"&nbsp;&nbsp;&nbsp;&nbsp;配置ID:");
		$("#reward_args_label_1", navTab.getCurrentPanel())
				.removeAttr("hidden");
		$("#reward_args_1", navTab.getCurrentPanel()).removeAttr("hidden");
	}
	if (obj.value == "钻石") {
		$("#reward_args_label", navTab.getCurrentPanel()).html(
				"&nbsp;&nbsp;&nbsp;&nbsp;钻石数量:");
		$("#reward_args_label_1", navTab.getCurrentPanel()).attr("hidden",
				"hidden");
		$("#reward_args_1", navTab.getCurrentPanel()).attr("hidden", "hidden");

	}
	if (obj.value == "金币") {
		$("#reward_args_label", navTab.getCurrentPanel()).html(
				"&nbsp;&nbsp;&nbsp;&nbsp;金币数量:");
		$("#reward_args_label_1", navTab.getCurrentPanel()).attr("hidden",
				"hidden");
		$("#reward_args_1", navTab.getCurrentPanel()).attr("hidden", "hidden");
	}

}

var reward_count = 0;// 用于奖励行数标识
var role_count = 0;
/**
 * 点击添加奖励,添加奖励到textArea中
 */
function addReward(id) {
	var reward_label = $("#reward_args_label", navTab.getCurrentPanel());
	var reward_args_1 = $("#reward_args_1", navTab.getCurrentPanel());
	var type = "道具";
	var typeNum = "1";
	if (reward_label.html().lastIndexOf("金币") != -1) {
		type = "金币";
		typeNum = "2";
	}
	
	if (reward_label.html().lastIndexOf("钻石") != -1) {
		type = "钻石";
		typeNum = "3";
	}

	var newreward = $(id, navTab.getCurrentPanel());
	if (newreward.val() == "") {
		alert(type + "参数不能为空!");
		return;
	}
	
	var para = typeNum + "," + newreward.val();

	if (type == "道具") {
		if (reward_args_1.val() == "") {
			alert(type + "数量不能为空!");
			return;
		}
		para += "," + reward_args_1.val();
	}

	var rewardList = $("#reward_list", navTab.getCurrentPanel());
	rewardList.removeAttr("hidden");
	$("#reward_hr_start", navTab.getCurrentPanel()).removeAttr("hidden");
	$("#reward_hr_end", navTab.getCurrentPanel()).removeAttr("hidden");
	// $("#reward_list_label", navTab.getCurrentPanel()).removeAttr("hidden");
	// 增加textArea行数
	// var oldRows = rewardList.attr("rows");
	// rewardList.attr("rows",parseInt(oldRows)+1);

	// reward: jQuery.fn.jQuery.init[1], 靠是个数组, html() 取得第一个匹配元素的html内容 ,
	// val返回的是数组
	var append = type + ":" + newreward.val();
	if (type == "道具") {
		append = append + "/" + reward_args_1.val();
	}

	
	// rewardList.html(xx);
	var append = "<tr id='rwd_tr_" + reward_count + "' para='"+para+"'><td>" + append
			+ "</td><td><input type='button' value='删除' onclick='deleteReward("
			+ "\"#rwd_tr_" + reward_count + "\")';></td></tr>";
	// alert(append);
	rewardList.after(append);
	newreward.val("");
	reward_count++;
}

/**
 * 删除奖励
 * 
 * @param id
 */
function deleteReward(id) {
	// alert(id);
	$(id).remove();
	reward_count--;
	if (reward_count == 0) {
		$("#reward_hr_start", navTab.getCurrentPanel())
				.attr("hidden", "hidden");
		$("#reward_hr_end", navTab.getCurrentPanel()).attr("hidden", "hidden");
	}
}

/**
 * 添加一个玩家到table中
 * @param sid 服务器ID
 * @param role_name
 * @param pid 
 */
function addRole(sid, role_name, pid) {

	if (containsRole(sid, role_name)) {
		alertMsg.error("该玩家已经存在,不可重复添加!");
		return;
	}

	$("#role_hr_start", navTab.getCurrentPanel()).removeAttr("hidden");
	$("#role_hr_end", navTab.getCurrentPanel()).removeAttr("hidden");
	var roleList = $("#role_list", navTab.getCurrentPanel());
	// var append = "<tr
	// id='role_tr_"+role_count+"'><td>"+role_name+"</td><td><input
	// type='button' value='删除'
	// onclick='deleteRole("+"\"#role_tr_"+role_count+"\")';></td></tr>";
	var append = "<tr id='role_tr_" + role_count + "' sid='" + sid + "' pid='"+pid+"'><td>"
			+ role_name
			+ "</td><td>"+sid+"服</td><td><input type='button' value='删除' onclick='deleteRole("
			+ "\"#role_tr_" + role_count + "\")';></td></tr>";
	// alert(append);
	roleList.after(append);
	role_count++;
}

/**
 * 判断玩家是否已经添加到table中
 * 
 * @param sid
 * @param role_name
 * @returns {Boolean}
 */
function containsRole(sid, role_name) {
	// var table = $("#role_list tr", navTab.getCurrentPanel());
	var hasSid = false;
	var hasRole = false;
	var trs = $("tr[id^='role_tr_']", navTab.getCurrentPanel());
	for (var i = 0; i < trs.length; i++) {
		var tr = $(trs[i]);
		var xxxxx = tr.attr("sid");
		if (tr.attr("sid") == sid) {
			hasSid = true;
		} else {
			hasSid = false;
		}

		if (tr.children().first().text() == role_name) {
			hasRole = true;
		} else {
			hasRole = false;
		}
	}

	if (hasSid == true && hasRole == true) {
		return true;
	} else {
		return false;
	}

}

function deleteRole(id) {
	$(id).remove();
	role_count--;
	if ($("tr[id^='role_tr_']", navTab.getCurrentPanel()).length == 0) {
		$("#role_hr_start", navTab.getCurrentPanel()).attr("hidden", "hidden");
		$("#role_hr_end", navTab.getCurrentPanel()).attr("hidden", "hidden");
	}
}

/**
 * 构造奖励数据
 */
function updateRewardInfo() {
	var trs = $("tr[id^='role_tr_']", navTab.getCurrentPanel());
	var rewardRoles = $("#rewardRoles", navTab.getCurrentPanel());
	var s1 = ""

	for (var i = 0; i < trs.length; i++) {
		if (s1 != "") {
			s1 += ";";
		}

		var tr = $(trs[i]);
		s1 += tr.attr("sid");
		s1 += ",";
//		s1 += tr.children().first().text();
//		s1 += ",";
		s1 += tr.attr("pid");
	}
	rewardRoles.val(s1);

	// =============================

	var trs = $("tr[id^='rwd_tr_']", navTab.getCurrentPanel());
	var rewardList = $("#rewardList", navTab.getCurrentPanel());
	s1 = ""
	for (var i = 0; i < trs.length; i++) {
		if (s1 != "") {
			s1 += ";";
		}
		var tr = $(trs[i]);
		s1 += tr.attr("para");
	}
	rewardList.val(s1);
}
